package com.jy.modules.cims.component.credit.creditor.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.jy.modules.cims.common.sysUtils.SpringContextUtils;
import com.jy.modules.cims.data.dao.RepaymentPlanDao;
import com.jy.modules.cims.data.dto.credit.CreditSettleParam;
import com.jy.modules.cims.data.dto.credit.CreditValueChangedParam;

@Service("com.jy.modules.cims.component.credit.creditor.common.SettlePlan")
public class SettlePlan implements ISettlePlan {

    ICalculateSettlePlan calculateSettlePlan;

    @Autowired
    private RepaymentPlanDao repaymentPlanDao;

    @Autowired
    public SpringContextUtils springContextUtil;

    @Override
    public void initSettlePlan(final CreditSettleParam creditSettleParam) throws Exception {
        String type = creditSettleParam.getRepayType();
        if (!StringUtils.hasText(type)) {
            throw new Exception("未配置还款方式！");
        }
        if ("02".equals(type)) {
            calculateSettlePlan = springContextUtil
                    .getBean("com.jy.modules.cims.component.credit.creditor.common.DEBXCalculate");
        } else if ("03".equals(type)) {
            calculateSettlePlan = springContextUtil
                    .getBean("com.jy.modules.cims.component.credit.creditor.common.XXHBCalculate");
        }
        // 计算结算计划
        List<Map<Object, Object>> list = calculateSettlePlan.calculate(creditSettleParam);
        // 保存
        repaymentPlanDao.initSettlePlan(list);
        // 更新债权状态
        Map<Object, Object> paramMap = new HashMap<Object, Object>();
        paramMap.put("credit_code", creditSettleParam.getCreditCode());
        paramMap.put("transfer_id", creditSettleParam.getTransferId());
        repaymentPlanDao.updateCreditDetialStatus(paramMap);
        
        paramMap = null;
    }

    @Override
    public void creditValueChanged(CreditValueChangedParam creditValueChangedParam) {
        // 债权价值变更后，更新结算计划的应还本，应还息。
        Map<Object, Object> paramMap = new HashMap<Object, Object>();
        paramMap.put("dto", creditValueChangedParam);
        repaymentPlanDao.updateCreditValueChanged(paramMap);
    }

}
